﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.Text;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    private void checkLogin(string username, string password)
    {
        User u = new User();        
        u.Password = password;
        password = u.Encrypt_Pass();


        if (!u.checkLogin(username, password))
        {
            //mpOKMsg.ShowMessage("Login fail!");
            lMessage.Text="Login fail!";
            msgBox.ShowOnPageLoad = true;
        }
        else
        {
            Session["username"] = u.UserName;
            Session["email"] = u.Email;
            Session["fullname"] = u.FullName;
            Session["groupid"] = u.GroupID;
            Session["phongbanid"] = u.PhongBanID;
            Session["tenphongban"] = u.TenPhongBan;
            Session["ngaynhanviec"] = u.NgayNhanViec;


            if (u.GroupID == 1)
                Response.Redirect("index.aspx");
            if (u.GroupID == 2)
                Response.Redirect("manager/index.aspx");
            if (u.GroupID == 3)
                Response.Redirect("admin/index.aspx");
            if (u.GroupID == 4)
                Response.Redirect("superadmin/index.aspx");
        }

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {

        if (txtUserName.Text == "")      
        {
            lMessage.Text = "Please enter username.";
            msgBox.ShowOnPageLoad = true;

        }
        else if (txtPassword.Text == "")
        {
      
            lMessage.Text = "Please enter password.";
            msgBox.ShowOnPageLoad = true;
        }
        

            else checkLogin(txtUserName.Text, txtPassword.Text);
    }
    protected void lForgotPassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("users/get_password.aspx");
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {

        Response.Redirect("users/register.aspx");
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        msgBox.ShowOnPageLoad = false;
    }
}
